﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;
using System.Collections.Generic;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ProjectStatistics : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBind_ProjectStatistics(0);
            }
        }

        protected void DataBind_ProjectStatistics(int recordcount)
        {
            if (Session["MerchantId"] != null)
            {
                int merchantId = int.Parse(Session["MerchantId"].ToString());
                if (recordcount == 0)
                {
                    this.AspNetPager_Content.CurrentPageIndex = 1;
                }
                int count = recordcount;
                int pageIndex = this.AspNetPager_Content.CurrentPageIndex;
                int pageSize = this.AspNetPager_Content.PageSize;

                this.GridView_Project.DataSource = ServiceLocator.ILogService.GetProjectReporting(merchantId, pageIndex, pageSize, ref count);
                this.GridView_Project.DataBind();
                this.AspNetPager_Content.RecordCount = count;
            }

        }

        protected void GridView_Project_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                IList project = (IList)e.Row.DataItem;

                Label lbNum = (Label)e.Row.FindControl("lbNum");
                lbNum.Text = (e.Row.RowIndex + 1).ToString();

                Label lbProjectName = (Label)e.Row.FindControl("lbProjectName");
                lbProjectName.Text = Convert.ToString(project[1]);

                Label lbStatus = (Label)e.Row.FindControl("lbStatus");
                switch (Convert.ToString(project[2]))
                {
                    case "1": lbStatus.Text = (String)GetGlobalResourceObject("Default", "UNPUBLISHED_UNAUDITED"); break;
                    case "2": lbStatus.Text = (String)GetGlobalResourceObject("Default", "PUBLISHED_UNAUDITED"); break;
                    case "3": lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDING"); break;
                    case "4": lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDED_SUCCESS"); break;
                    case "5": lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDED_FAILED");  break;
                }

                Label lbSendNum = (Label)e.Row.FindControl("lbSendNum");
                lbSendNum.Text = Convert.ToString(project[3]);

                Label lbUseNum = (Label)e.Row.FindControl("lbUseNum");
                lbUseNum.Text = Convert.ToString(project[4]);

                Label lbRate = (Label)e.Row.FindControl("lbRate");
                lbRate.Text = Math.Round(Convert.ToDecimal(project[5]) * 100, 2).ToString() + "%";

                HyperLink hlSendLog = (HyperLink)e.Row.FindControl("hlSendLog");
                hlSendLog.NavigateUrl = "SendDetails.aspx?BackUrl=ProjectStatistics.aspx&ProjectId=" + Convert.ToString(project[0]);

                HyperLink hlUseLog = (HyperLink)e.Row.FindControl("hlUseLog");
                hlUseLog.NavigateUrl = "UseDetails.aspx?BackUrl=ProjectStatistics.aspx&ProjectId=" + Convert.ToString(project[0]);

                if (lbSendNum.Text == "0")
                {
                    hlSendLog.Visible = false;
                }
                if (lbUseNum.Text == "0")
                {
                    hlUseLog.Visible = false;
                }
            }
        }

        protected void AspNetPager_Content_PageChanged(object sender, EventArgs e)
        {
            DataBind_ProjectStatistics(this.AspNetPager_Content.RecordCount);
        }
    }
}

